package com.ecut.xzh.controller;

import com.ecut.xzh.dto.ArticleDTO;
import com.ecut.xzh.dto.ArticlePageDTO;
import com.ecut.xzh.result.PageResult;
import com.ecut.xzh.result.Result;
import com.ecut.xzh.service.ArticleService;
import com.ecut.xzh.vo.ArticleDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author XZH
 */
@RequestMapping("/article")
@RestController
@Slf4j
public class ArticleController {

    @Resource
    private ArticleService articleService;

    /**
     * 新增文章
     * @param articleDTO
     * @return
     */
    @PostMapping("/add")
    public Result addArticle(@RequestBody ArticleDTO articleDTO){
        log.info("新增文章:{}", articleDTO);

        articleService.addArticle(articleDTO);
        return Result.success();
    }

    /**
     * 文章分页查询
     * @param articlePageDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> pageArticle(ArticlePageDTO articlePageDTO){
        log.info("开始文章分页查询");
        PageResult pageResult = articleService.pageArticle(articlePageDTO);
        return Result.success(pageResult);
    }

    /**
     * 获取文章详细信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<ArticleDetailVO> getArticleById(@PathVariable Long id){
        log.info("获取详细信息的文章:{}", id);
        ArticleDetailVO articleDetailVO = articleService.getArticleById(id);
        return Result.success(articleDetailVO);
    }

    /**
     * 点赞文章
     * @param id
     * @return
     */
    @PutMapping("/like/{id}")
    public Result likeArticle(@PathVariable Long id){
        log.info("点赞文章:{}", id);
        articleService.likeArticle(id);
        return Result.success();
    }
}
